﻿using System;
using Terraria;
using Terraria.ModLoader;
using Terraria.ID;

namespace 减少材料成本
{
    public class 减少材料成本 : ModSystem
    {
        public override void PostAddRecipes()
        {
            float costMultiplier = 减少材料成本设置Helpers.Instance.Material_Cost / 100f;

            foreach (Recipe recipe in Main.recipe)
            {
                if ((recipe?.createItem?.IsAir) == false && recipe.requiredItem != null)
                {
                    if (减少材料成本设置Helpers.Instance.No_Craft_Station)
                    {
                        recipe.requiredTile.Clear();
                    }
                    foreach (Item material in recipe.requiredItem)
                    {
                        if ((material?.IsAir) == false)
                        {
                            if (减少材料成本设置Helpers.Instance.Coin_Enable || !IsCoin(material.type))
                                material.stack = Math.Max(1, (int)Math.Ceiling(material.stack * costMultiplier));
                        }
                    }
                }
            }
        }

        private static bool IsCoin(int itemType)
        {
            return itemType == ItemID.CopperCoin || itemType == ItemID.SilverCoin
                || itemType == ItemID.GoldCoin || itemType == ItemID.PlatinumCoin;
        }
    }
}